<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .list {
            display: flex;
            text-align: center;
        }

        .page1 {
            margin-left: 20px;
        }
    </style>
</head>

<body>
    <div class="list"></div>
    <button class="prev">上一页</button>
    <button class="next">下一页</button>
    <span class="page1"></span>
</body>

</html>
<script src="./promise.js"></script>
<script>
    var page = 1    //保存当前的页码
    var prev = document.querySelector('.prev')      //获取上一页的标签
    var next = document.querySelector('.next')      //获取下一页的标签

    //让第一页显示
    ajax('get', 'https://yantianfeng.com/api/emotion', { page: 1, count: 10 }).then((data) => {
        var str = data.emotions.map((item, index) => {
            return `<div><img src="${item.url}"/><div>${item.title}</div></div>`
        }).join('')
        prev.disabled = true   //button禁用
        document.querySelector('.list').innerHTML = str
        document.querySelector('.page1').innerHTML = `1/4`
    })

    //点击上一页，获取上一页的表情，渲染表情
    var prev = document.querySelector('.prev')      //获取上一页的标签
    prev.addEventListener('click', function () {
        next.disabled = false     //点击上一页，下一页按钮释放
        if (page > 1) {
            ajax('get', 'https://yantianfeng.com/api/emotion', { page: --page, count: 10 }).then((data) => {
                var str = data.emotions.map((item, index) => {
                    return `<div> <img src="${item.url}"/> <div>${item.title}</div> </div>`
                }).join('')
                //渲染表情
                document.querySelector('.list').innerHTML = str
            })
            if (page == 1) {
                this.disabled = true    //当页数为1时禁用上一页按钮
            }
            document.querySelector('.page1').innerHTML = `${page}/4`
        }
    })

    //点击下一页，获取下一页的表情，渲染表情
    var next = document.querySelector('.next')      //获取下一页的标签
    next.addEventListener('click', function () {
        prev.disabled = false     //点击下一页，上一页按钮释放
        if (page < 4) {
            ajax('get', 'https://yantianfeng.com/api/emotion', { page: ++page, count: 10 }).then((data) => {
                var str = data.emotions.map((item, index) => {
                    return `<div> <img src="${item.url}"/> <div>${item.title}</div> </div>`
                }).join('')
                //渲染表情
                document.querySelector('.list').innerHTML = str
            })
            if (page == 4) {
                this.disabled = true    //当页数为4时禁用下一页按钮
            }
            document.querySelector('.page1').innerHTML = `${page}/4`
        }
    })
</script>